package cn.iocoder.yudao.module.digital.controller.admin.csc;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.digital.controller.admin.csc.vo.*;
import cn.iocoder.yudao.module.digital.dal.dataobject.csc.CscDO;
import cn.iocoder.yudao.module.digital.service.csc.CscService;

@Tag(name = "管理后台 - 软著")
@RestController
@RequestMapping("/digital/csc")
@Validated
public class CscController {

    @Resource
    private CscService cscService;

    @PostMapping("/create")
    @Operation(summary = "创建软著")
    @PreAuthorize("@ss.hasPermission('digital:csc:create')")
    public CommonResult<Integer> createCsc(@Valid @RequestBody CscSaveReqVO createReqVO) {
        return success(cscService.createCsc(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新软著")
    @PreAuthorize("@ss.hasPermission('digital:csc:update')")
    public CommonResult<Boolean> updateCsc(@Valid @RequestBody CscSaveReqVO updateReqVO) {
        cscService.updateCsc(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除软著")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('digital:csc:delete')")
    public CommonResult<Boolean> deleteCsc(@RequestParam("id") Integer id) {
        cscService.deleteCsc(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得软著")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('digital:csc:query')")
    public CommonResult<CscRespVO> getCsc(@RequestParam("id") Integer id) {
        CscDO csc = cscService.getCsc(id);
        return success(BeanUtils.toBean(csc, CscRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得软著分页")
    @PreAuthorize("@ss.hasPermission('digital:csc:query')")
    public CommonResult<PageResult<CscRespVO>> getCscPage(@Valid CscPageReqVO pageReqVO) {
        PageResult<CscDO> pageResult = cscService.getCscPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CscRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出软著 Excel")
    @PreAuthorize("@ss.hasPermission('digital:csc:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCscExcel(@Valid CscPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CscDO> list = cscService.getCscPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "软著.xls", "数据", CscRespVO.class,
                        BeanUtils.toBean(list, CscRespVO.class));
    }

}